﻿<%@ WebHandler Language="C#" Class="Employee" %>

using System;
using System.Web;
using System.Web.SessionState;
public class Employee : IHttpHandler,IRequiresSessionState
{
    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string title = context.Request["title"];
        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "add":
                    if (Add(context.Request["employeename"],context.Request["sex"],
                        context.Request["age"],context.Request["birthday"],
                        context.Request["origin"],context.Request["homeaddr"],
                        context.Request["idcard"],context.Request["qq"],
                        context.Request["email"],context.Request["dwz.id"],
                        context.Request["dwz.orgname"], context.Request["position"],
                        context.Request["remark"], context.Request["mobile"]))
                    {
                        context.Response.Write(success("", "EmployeesList"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
                case "delete":
                    if (Del(context.Request["ids"]))
                    {
                        context.Response.Write(success("", "EmployeesList"));
                    }
                    else
                    {
                        context.Response.Write(failure("删除用户失败!"));
                    }
                    break;
                case "updata":
                    if (Updata(context.Request["employeeid"], context.Request["employeename"],
                        context.Request["sex"], context.Request["age"],
                        context.Request["birthday"],context.Request["origin"],
                        context.Request["homeaddr"], context.Request["idcard"],
                        context.Request["qq"],context.Request["email"],
                        context.Request["dwz.id"], context.Request["dwz.orgname"],
                        context.Request["position"], context.Request["remark"], context.Request["mobile"]))
                    {
                        context.Response.Write(success("", "EmployeesList"));
                    }
                    else
                    {
                        context.Response.Write(failure("修改失败!"));
                    }
                    break;
            }
        }
    }

    public bool Del(string ids)
    {
        var employeesManager = appContext.GetObject("S_EmployeesManager") as GONES.IBLL.IS_EmployeesManager;
        string[] strs = ids.Split(',');
        try
        {
            foreach (string str in strs)
            {
                employeesManager.Delete(str);
            }
            return true;
        }
        catch { return false; }
    }

    private GONES.Model.S_Employees GetEmployeesModel(string id, string employeename, string sex, string age, string birthday, string origin,
        string homeaddr, string idcard, string qq, string email, string orgid, string orgname, string position, string remark, string mobile)
    {
        var employeeInfoManager = appContext.GetObject("S_EmployeesManager") as GONES.IBLL.IS_EmployeesManager;
        GONES.Model.S_Employees emps = new GONES.Model.S_Employees();
        if (id != "")
            emps = employeeInfoManager.Get(id);
        emps.ID = id;
        emps.EmployeeName = employeename;
        emps.Sex =int.Parse(sex);
        emps.Age = int.Parse(age);
        if(!string.IsNullOrEmpty(birthday))
            emps.Birthday =DateTime.Parse(GONES.Tools.Common.Tools.GetStandardDate (birthday));
        emps.Origin = origin;
        emps.HomeAddr = homeaddr;
        emps.IdCard = idcard;
        emps.QQ = qq;
        emps.Email = email;
        emps.Mobile = mobile;
        emps.DepartId = orgid;
        emps.DepartName = orgname;
        emps.Position = position;
        emps.ReMark = remark;
        return emps;
    }
    public bool Add(string employeename, string sex, string age, string birthday,string origin,
        string homeaddr, string idcard, string qq, string email, string orgid, string orgname, string position, string remark, string mobile)
    {
        var empInfoManager = appContext.GetObject("S_EmployeesManager") as GONES.IBLL.IS_EmployeesManager;
        GONES.Model.S_Employees emps = new GONES.Model.S_Employees();
        try
        {
            string id = "";
            emps = GetEmployeesModel(id, employeename, sex, age, birthday, origin,
          homeaddr,  idcard, qq, email, orgid,  orgname , position,  remark,mobile);
            emps.ID = GONES.Tools.Common.Rnd.Guid.ToString();
            emps.CreateUser = loginInfo.UserName;
            
            empInfoManager.Add(emps);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool Updata(string id, string employeename, string sex, string age, string birthday, string origin,
        string homeaddr, string idcard, string qq, string email, string orgid, string orgname, string position, string remark, string mobile)
    {
        var empInfoManager = appContext.GetObject("S_EmployeesManager") as GONES.IBLL.IS_EmployeesManager;
        GONES.Model.S_Employees emps = new GONES.Model.S_Employees();
        try
        {
            emps = GetEmployeesModel(id, employeename, sex, age, birthday, origin,
          homeaddr, idcard, qq, email, orgid, orgname, position, remark,mobile);
            emps.ModifyUser = loginInfo.UserName;
            empInfoManager.Edit(emps);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}